package com.molyfun.parents.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

import com.molyfun.parents.view.GenericProgressDialog;
import com.molyfun.parents.view.ZhiboLoadingDialog;

public class DialogHelper {
    private Activity mActivity;
    private AlertDialog mAlertDialog;

    public DialogHelper(Activity activity) {
        mActivity = activity;
    }

    public void alert(final String title, final String msg, final String positive,
                      final DialogInterface.OnClickListener positiveListener,
                      final String negative, final DialogInterface.OnClickListener negativeListener) {
        alert(title, msg, positive, positiveListener, negative, negativeListener, false);
    }

    public void alert(final String title, final String msg, final String positive,
                      final DialogInterface.OnClickListener positiveListener,
                      final String negative,
                      final DialogInterface.OnClickListener negativeListener,
                      final Boolean isCanceledOnTouchOutside) {
        dismissProgressDialog();

        mActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mActivity == null || mActivity.isFinishing()) {
                    return;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                if (title != null) {
                    builder.setTitle(title);
                }
                if (msg != null) {
                    builder.setMessage(msg);
                }
                if (positive != null) {
                    builder.setPositiveButton(positive, positiveListener);
                }
                if (negative != null) {
                    builder.setNegativeButton(negative, negativeListener);
                }
                mAlertDialog = builder.show();
                mAlertDialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside);
                mAlertDialog.setCancelable(false);
            }
        });
    }

    public void toast(final String msg, final int period) {
        mActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                UIUtils.showToast(mActivity, msg, period);
            }
        });
    }

    public void showProgressDialog(boolean showProgressBar, String msg) {
        showProgressDialog(msg, true, null, showProgressBar);
    }

    public void showProgressDialog(final String msg) {
        showProgressDialog(msg, true, null, true);
    }

    public void showProgressDialog(final String msg, final boolean cancelable,
                                   final OnCancelListener cancelListener,
                                   final boolean showProgressBar) {
        dismissProgressDialog();

        mActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mActivity == null || mActivity.isFinishing()) {
                    return;
                }
                try{
                    mAlertDialog = new GenericProgressDialog(mActivity);
                    mAlertDialog.setMessage(msg);
                    ((GenericProgressDialog) mAlertDialog).setProgressVisiable(showProgressBar);
                    mAlertDialog.setCancelable(cancelable);
                    mAlertDialog.setCanceledOnTouchOutside(cancelable);
                    mAlertDialog.setOnCancelListener(cancelListener);
                    mAlertDialog.show();
                    mAlertDialog.setCanceledOnTouchOutside(false);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void showZhiboProgressDialog(){
        dismissProgressDialog();

        mActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mActivity == null || mActivity.isFinishing()) {
                    return;
                }
                try{
                    mAlertDialog = new ZhiboLoadingDialog(mActivity);
                    mAlertDialog.setCancelable(false);
                    mAlertDialog.setCanceledOnTouchOutside(false);
                    mAlertDialog.show();
                    mAlertDialog.setCanceledOnTouchOutside(false);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void dismissProgressDialog() {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mAlertDialog != null && mAlertDialog.isShowing() && !mActivity.isFinishing()) {
                    mAlertDialog.dismiss();
                    mAlertDialog = null;
                }
            }
        });
    }

}
